package org.example.tree;

/**
 * @author liyishan
 * @date 2024/7/19 13:33
 * @apiNote
 */

public class Test00993IsCousins {
    int x;
    TreeNode xParent;
    int xDepth;
    boolean xFound = false;

    int y;
    TreeNode yParent;
    int yDepth;

    boolean yFound = false;

    public boolean isCousins(TreeNode root, int x, int y) {
        this.x = x;
        this.y = y;
        dfs(root,0,null);
        return xDepth == yDepth && xParent != yParent;
    }

    public void dfs(TreeNode node,int depth,TreeNode parent){
        if(node == null){
            return;
        }
        if(node.val == x){
            xParent = parent;
            xDepth = depth;
            xFound = true;
        }else if(node.val == y){
            yParent = parent;
            yDepth = depth;
            yFound = true;
        }
        if(xFound && yFound){
            return;
        }
        dfs(node.left,depth+1,node);
        if(xFound && yFound){
            return;
        }
        dfs(node.right,depth+1,node);

    }
}
